//
// Created by nicemoe on 2021/8/7.
//

#include  <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;
const int SIZE = 60;

/**
 * scores.txt 内容如下
 *
 * 18 19 18.5 13.5 14
 * 16 19.5 20 18 12 18.5
 *  17.5
 *
 *
 *
 * 若编辑器不自动加换行符需要手动换行
 * @return
 */
int main()
{

    char filename[SIZE];
    ifstream inFile;
    cout << "Enter name of data file: ";
    cin.getline(filename, SIZE);
    inFile.open(filename);
    if (!inFile.is_open())
    {
        cout << "Could not open the file " << filename << endl;
        cout << "pROGRAM TERMINATING. \n";
        exit(EXIT_FAILURE);
    }
    double value;
    double sum = 0.0;
    int count = 0;

//    inFile >> value;
    //inFile.good() 该方法在没有任务错误且没有遇到结束符号的时候返回为true，
    //inFile >> value 的返回值为inFile 在需要判断bool的时候 inFile的结果是inFile.good()
    while (inFile >> value)
    {
        ++count;
        sum += value;

    }
    if (inFile.eof()) cout << "End of file reached. \n";
    else if (inFile.fail()) cout << "Input terminated by data mismatch.\n";
    else cout << "Input terminated for unknown reason. \n";
    if (count == 0) cout << "No data processed.\n";
    else
    {
        cout << "Items read: " << count << endl;
        cout << "Sum: " << sum << endl;
        cout << "Average: " << sum / count << endl;
    }
    inFile.close();
    return 0;
}

/*
结果如下
Enter name of data file: scores.txt
End of file reached.
Items read: 12
Sum: 204.5
Average: 17.0417
*/